package com.situ.web.controller;

import com.situ.web.util.JSONResult;
import com.situ.web.util.JSONUtil;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "checkUserName":
                checkUserName(req, resp);
                break;
        }
    }

    // {}   [{},{}]
    //{"code":0,"msg":"该用户名可用","data":null,}
    private void checkUserName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.checkUserName");
        String name = req.getParameter("name");
        if (name.equals("zhangsan")) {//代表这个名字已经被占用
            /*JSONResult jsonResult = new JSONResult();
            jsonResult.setCode(1);
            jsonResult.setMsg("该用户名已经存在，请更换一个");*/

            JSONUtil.obj2Json(JSONResult.error("该用户名已经存在，请更换一个"), resp);
        } else {
            /*JSONResult jsonResult = new JSONResult();
            jsonResult.setCode(0);
            jsonResult.setMsg("该用户名可用");*/

            JSONUtil.obj2Json(JSONResult.ok("该用户名可用"), resp);
        }
    }
}
